# coding:utf-8
from mongo import diarys, users
from flask_restful import Resource
from flask import request,Response
from bson.json_util import dumps
from bson import json_util
from datetime import datetime
import pymongo
from bson.objectid import ObjectId



class DiaryUser(Resource):

    def get(self, user):
        return Response(dumps(diarys.find({'user':user}).sort('createtime', -1)),mimetype="application/json")

class DiaryDay(Resource):

    def get(self, day):
        return Response(dumps(diarys.find({'day':day})),mimetype="application/json")

class DiaryList(Resource):

    def get(self):
        #logs = diarys.find().sort('createtime', -1)
        #print([{'name':users.find_one({'name':log['user']})['desc'], 'day': log['day'], 'summary': log['summary'], 'content': log['content'], 'createtime': log['createtime']} for log in logs])
        return Response(dumps(diarys.find().sort('createtime', -1), default=json_util.default), mimetype="application/json")
    
    def post(self):
        log = request.get_json()
        result = diarys.insert_one(log)
        if (result):
            diarys.update_one({'_id': result.inserted_id}, {'$set': {'createtime': datetime.utcnow()}})
        return str(result.inserted_id)

class LogList(Resource):
    '''put method for log by id'''
    def put(self, id):
        log = request.get_json()
        result = None
        if (id):
            result = diarys.update_one({'_id': ObjectId(id)}, {'$set': log})
            status = 200 if 1 == result.matched_count and 1 == result.modified_count else 400
            return Response(dumps(result.raw_result), status=status, mimetype="application/json")
        else:
            return Response('arguments null exception', status=400)
    
    def delete(self, id):
        if (id):
            result = diarys.delete_one({'_id': ObjectId(id)})
            status = 200 if result.deleted_count == 1 else 400
            return Response(dumps(result.raw_result), status = status, mimetype="application/json")
        else:
            return Response('arguments null exception', status=400)

class DiaryToday(Resource):
    """diarys today"""

    def get(self, user, day):
        return Response(dumps(diarys.find({'user':user,'day':day})), mimetype="application/json")

    def put(self, user, day):
        if day and user:
            log = request.get_json()
            result = diarys.update_one({'user':user, 'day':day},{'$set': log})
            status = 200 if 1 == result.matched_count and 1 == result.modified_count else 400
            return Response(dumps(result.raw_result), status=status, mimetype="application/json")
        else:
            return Response('user and day can not be empty', status=400)

    def delete(self, user, day):
        if day and user:
            result = diarys.delete_one({'user':user, 'day':day})
            status = 200 if result.deleted_count == 1 else 400
            return Response(dumps(result.raw_result), status = status, mimetype="application/json")
        else:
            return Response("user and day can not be none", status=400)

if __name__ == '__main__':
    diarys.insert_one({'user':'ZhangPeng', 'day': '2017-7-4','summary': 'summary 1', 'content':'content 1', 'createtime': datetime.utcnow()})
    diarys.insert_one({'user':'ZhangPeng', 'day': '2017-7-4','summary': 'summary 2', 'content':'content 2', 'createtime': datetime.utcnow()})
    diarys.insert_one({'user':'ZhangPeng', 'day': '2017-7-4','summary': 'summary 3', 'content':'content 3', 'createtime': datetime.utcnow()})
    diarys.insert_one({'user':'ZhangPeng', 'day': '2017-7-4','summary': 'summary 4', 'content':'content 4', 'createtime': datetime.utcnow()})
